package org.example.studyonline.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.example.studyonline.mapper.UserMapper;
import org.example.studyonline.pojo.DTO.CommentDTO;
import org.example.studyonline.pojo.VO.UserInfoVO;
import org.example.studyonline.pojo.Result;
import org.example.studyonline.pojo.User;
import org.example.studyonline.pojo.DTO.LoginDTO;
import org.example.studyonline.pojo.DTO.RegisterDTO;
import org.example.studyonline.service.CommentService;
import org.example.studyonline.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    @Resource
    private CommentService commentService;

    @PostMapping("/register")
    public Result register(@RequestBody @Validated RegisterDTO registerDTO) {

        String result = userService.register(registerDTO);
        if (result == null) {
            return Result.success("注册成功",null);

        } else {
            return Result.error(result);
        }
    }

    @PostMapping("/login")
    public Result login(@RequestBody @Validated LoginDTO loginDTO) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(loginDTO.getEmail(), loginDTO.getPassword());
        try {
            subject.login(token);
            subject.getSession().setTimeout(1800000L);
            //记录用户id
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("email", loginDTO.getEmail());
            User user = userMapper.selectOne(queryWrapper);
            if (user.getIsFrozen().equals("1")){
                throw new RuntimeException("该用户已被冻结，请联系管理员!");
            }
            subject.getSession().setAttribute("userId", user.getUserID());
            return Result.success("登录成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    @GetMapping("/info")
    public Result<UserInfoVO> UserInfo(){
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getSession().getAttribute("userId");
        UserInfoVO info = userService.getUserInfo(userId);
        if(info == null){
            return Result.error("用户不存在");
        }
        return Result.success("获取用户信息成功",info);

    }


    //登录状态验证
    @GetMapping("/check-login")
    public Result checkLogin(){
        Subject subject = SecurityUtils.getSubject();
        if(subject.isAuthenticated()){
            return Result.success("登录状态验证成功",1);
        }
        return Result.error("登录状态验证失败",0);
    }

    //用户发表评论
    @RequiresAuthentication
    @PostMapping("/comment")
    public Result comment(@RequestBody @Validated CommentDTO commentDTO){
        Map map = commentService.comment(commentDTO);
        return Result.success("评论成功",map);
    }

}
